go

exec dbo.p_drop_proc_if_exists 'meta_add_guid_column';

go

--Function of adding new guid column
create procedure meta_add_guid_column(
    --Table id
    @a_table_id binary(16),
    --Column id
    @a_column_id binary(16),
    --Column name
    @a_column_name varchar(max))
with execute as owner
as
begin
	set nocount on;
	declare
	--Table name
    @a_table_name varchar(26),
    --Result
    @a_result numeric(1);
    
    exec dbo.p_check_col_not_exists @a_table_id, @a_column_id, @a_column_name, @a_result output;
    
    if @a_result = 0 return;
    
    set xact_abort on;
    begin tran;

    set @a_table_name = dbo.p_get_table_name(@a_table_id);
    
    --Changing the metadata
    insert into meta_guid_columns(column_id, table_id, column_name,
        order_index, customization_level)
    values (@a_column_id, @a_table_id, @a_column_name,
        dbo.p_generate_order_index(@a_table_id), 1);
        
    --Dropping table view
    exec dbo.p_drop_view_if_exists @a_table_name;
         
    exec ('alter table tbl_' 
    + @a_table_name
    + ' add ' + @a_column_name
    + ' binary(16)');
         
    --Creating table view
    exec dbo.p_create_table_view @a_table_id;

	commit;
end;

go

exec dbo.p_drop_proc_if_exists 'meta_set_guid_default';

go

--Function of setting column default value
create procedure meta_set_guid_default(
    --Column id
    @a_column_id binary(16),
    --Default value
    @a_default_value binary(16))
with execute as owner
as
begin
	set nocount on;
	declare
	--Table id
    @a_table_id binary(16),
    --Table name
    @a_table_name varchar(26),
    --Column name
    @a_column_name varchar(26),
    --Customization level
    @a_customization_level numeric(10),
    --Result
    @a_result numeric(1),
    --Default value str
    @a_default_value_str varchar(32),
    --Column id str
    @a_column_id_str varchar(32),
    --Old default value
    @a_old_default_value binary(16);
    
    exec dbo.p_check_col_type @a_column_id, 'meta_guid_columns', 'guid', @a_result output;
    
    if @a_result = 0 return;
    
    set @a_column_id_str = dbo.p_bin_to_str(@a_column_id);
    set @a_default_value_str = dbo.p_bin_to_str(@a_default_value);
    
    --Reading column properties
    select @a_table_id = table_id, @a_column_name = column_name,
    @a_customization_level = customization_level,
    @a_old_default_value = default_value
    from meta_guid_columns
    where column_id = @a_column_id;
    
    --If this is a system column
    if @a_customization_level = 0
    begin
        raiserror('Changing guid system column default value is forbidden (column_id = %s) (#error_code = 20008)',
        16, 1, @a_column_id_str);
        return;
    end;
    
    --If there is no need for change
    if (@a_old_default_value = @a_default_value) or
    (@a_old_default_value is null and @a_default_value is null)
		return;
    
    set @a_table_name = dbo.p_get_table_name(@a_table_id);
    
    set xact_abort on;
    begin tran;
    
    --If there is an old default value
    if @a_old_default_value is not null
		--Dropping the old default value
		exec ('alter table tbl_' 
        + @a_table_name
        + ' drop constraint def_' + @a_column_id_str);
    
    --Updating the metadata
    update meta_guid_columns
    set default_value = @a_default_value
    where column_id = @a_column_id;
    
    --If there is a new default value
    if @a_default_value is not null
        exec ('alter table tbl_' 
        + @a_table_name
        + ' add constraint def_' + @a_column_id_str
        + ' default 0x' + @a_default_value_str + ' for ' + @a_column_name);        
    
    commit;
end;